Passed
Pull Request — master (#216)
by
unknown
02:29
created

UpdateContactCommandHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 20
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 21 2
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { UpdateContactCommand } from './UpdateContactCommand';
4
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
5
import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException';
6
7
@CommandHandler(UpdateContactCommand)
8
export class UpdateContactCommandHandler {
9
  constructor(
10
    @Inject('IContactRepository')
11
    private readonly contactRepository: IContactRepository
12
  ) {}
13
14
  public async execute(command: UpdateContactCommand): Promise<void> {
15
    const {
16
      id,
17
      firstName,
18
      lastName,
19
      company,
20
      email,
21
      phoneNumber,
22
      notes
23
    } = command;
24
25
    const contact = await this.contactRepository.findOneById(id);
26
27
    if (!contact) {
28
      throw new ContactNotFoundException();
29
    }
30
31
    contact.update(firstName, lastName, company, email, phoneNumber, notes);
32
33
    await this.contactRepository.save(contact);
34
  }
35
}
36